/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package pay.biz;

import java.util.HashMap;
import java.util.Map;

import com.alipay.api.AlipayApiException;

import base.model.exception.BizHandleException;
import mt.Const;

/**
 * 业务动作分发器,执行逻辑工厂
 * 
 * @author taixu.zqq
 * @version $Id: Dispatcher.java, v 0.1 2014年7月24日 下午4:59:58 taixu.zqq Exp $
 */
public class Dispatcher {

    /** 通知业务处理工厂 */
    private static Map<PayNotifyTypeEnum, TradeNotifyExecutor> bizExecutorFactory = new HashMap<PayNotifyTypeEnum, TradeNotifyExecutor>();

    static {
        bizExecutorFactory.put(PayNotifyTypeEnum.APAY_TRADE_STATUS_SYNC, new TradeNotifyAliExecutor());
        bizExecutorFactory.put(PayNotifyTypeEnum.WXPAY_TRADE_STATUS_SYNC, new TradeNotifyWxExecutor());
    }

    /**
     * 根据业务参数获取业务执行器
     * 
     * @param params
     *            通知业务参数
     * @return ApayNotifyExecutor
     * @throws AlipayApiException
     */
    public static TradeNotifyExecutor getBizExecutor(PayTypeEnum payType, Map<String, String> params)
            throws AlipayApiException, BizHandleException {

        TradeNotifyExecutor executor = null;
        if (payType == PayTypeEnum.APAY_MOBILE) {
            // 获取通知类型
            PayNotifyTypeEnum msgTypeEnum = PayNotifyTypeEnum
                    .getEnumByCode(params.get(Const.ALIPAY_MSG_NOTIFY_TYPE));
            if (msgTypeEnum == null) {
                throw new AlipayApiException("无法取得通知类型！");
            }
            executor = bizExecutorFactory.get(msgTypeEnum);
            if (executor == null) {
                throw new BizHandleException("无法取得通知处理程序！");
            }
        } else {
            executor = bizExecutorFactory.get(PayNotifyTypeEnum.WXPAY_TRADE_STATUS_SYNC);
        }

        return executor;
    }

}
